<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: UserInfo.php 262 2011-07-23 15:51:14Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");

//Baogg_Db_Table just for one table,not multi,so can't chang to ModelTable
class UserInfo  extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name;
	protected $_db ;
	protected $_primary; 
	
	function __construct($model_id) {
		$this->_name =   'user';		
		$this->primary= $this->_primary='user_id';
		
		
		$this->modelTable=new ModelTable($model_id);		
		
		parent::__construct();
	}
	
	/*
	 *   get  data list
	 */
	function getList($where=array(),$order=array(),$limit=array(),$cols="*",$joins=array()) {
	    
		$select=$this->_db->select();
		$select->from($this->_name,$cols);
		
		
		$select->join($this->modelTable->getName(), "user.user_id=model.ontent_id", "*")	;
		
	    foreach((array)$where as $k=>$v){
	        if(is_int($v) || is_float($v)){
	           $select->where("$k =  ?",$v ) ; 
	        }else{
	           $select->where("$k like  ?",$v ) ;
	        }
	    }
	    if($order){
	       $select->order(array(" {$order['sort']} {$order['dir']} ")) ;  
	    }
		if($limit){
		    $select->limitPage($limit['start'],$limit['limit']) ;
		} 
      		  
	   $rs=$this->_db->fetchAll($select);
	  // $rs[0]['sub_table']='gg';
	   
	   return $rs;	
	}
	
	
/*
	 *   get  data list ,$model_table
	 */
	function getModelList($where=array(),$order=array(),$limit=array(),$col=array("main"=>"*","ext"=>"*")) {

		$select=$this->_db->select();
		$select->from($this->_name,@$col['main']);
				
		
		
		$select->join($this->modelTable->getName(), $this->db_prefix."user.user_id={$this->modelTable->getName()}.user_id", @$col['ext'])	;

		$arr_where_or=array();
	    foreach((array)$where as $k=>$v){
	    	if(is_int($k)){
	    		$select->where("$v");
	    	}else if(strpos(trim($k)," ")){
	    		$select->where("$k $v");
	    	}else if(is_int($v) || is_float($v)){
	           $select->where("$k =  ?",$v ) ; 
	        }else if(is_array($v)){
	        	 $select->where("$k in  (?)",$v ) ;
	        }else{
	        	if($this->is_query_value($v)){
	        		$arr_where_or[]=$this->_db->quoteInto("$k like ?",'%'.$this->get_query_value($v).'%');
	        	}else{
	           		$select->where("$k like  ?",'%'.$v.'%' ) ;
	        	}
	        }
	    }
	    if($arr_where_or){
	    	$select->where('('.implode(" or ",$arr_where_or).')');
	    }
	    if($order){
	       $select->order(array(" {$order['sort']} {$order['dir']} ")) ;  
	    }
		if($limit){
		    $select->limitPage($limit['start'],$limit['limit']) ;
		} 
      	try{	  
	   		$rs=$this->_db->fetchAll($select);
      	}catch(Exception $e){
      		echo $select;
      		echo $e->getMessage();
      		exit;
      	}
	  // $rs[0]['sub_table']='gg';
	   
	   return $rs;	
	}
	
	
	
	 

     
	


	function getSame($arr) {	
		$ret=array();
		
		return parent::getSame(array("user_id"=>$arr['user_id']));
			
	
	}
	
	
	
	function compile($str,$params=array()){
		preg_match_all("/\{\$([\w\d\.]+)\}/", $str, $matches);
		//echo '<pre>';print_r($params);exit;
		//foreach((array)$matches[1] as $k)
	}
	
	
	static function updateScore(){
		
		$Model=new Model();
		$dir=BAOGG_UPLOAD_DIR."rule/";
		$rs_model=$Model->getList(array("state"=>1,"main_table"=>"user"));

		foreach((array)$rs_model as $schema){
			//$arr_model[$schema['ext_table']]=array("name"=>$schema['model_name'],"score"=>0,"demo"=>"","items"=>array());
			$ModelTable=new ModelTable((int)$schema['model_id']);
			$rs_model_table=$ModelTable->getShowList();
			foreach((array)$rs_model_table as $k=>$v){
				$file=$dir.$schema['rule_id'].".php";
				$v['score']=include $file;
				$ModelTable->_update(array("score"=>$v['score']),$v['id']);			
			}
		}
	}
	

}
